package com.tvein.sanguo.controller.Game.user
{
	//flash
	import flash.net.LocalConnection;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	//pureMVC
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.fabrication.patterns.command.SimpleFabricationCommand;
	//custom
	import com.tvein.sanguo.common.CommonConstants;
	import com.tvein.sanguo.model.FilesProxy;
	import com.tvein.sanguo.model.SFSProxy;
//
	public class RoomChange extends SimpleFabricationCommand implements ICommand
	{
		private var filesData:FilesProxy;
		private var sfsData:SFSProxy;
		
		private var Music:String;
		private var BackGround:String;
		private var Canton:String;
		private var County:String;
		private var Fort:String;
		private var Place:String;
		private var RoomID:String;
		private var RoomName:String;
		private var RoomType:int;
		private var RoomTypeName:String;
		private var RoomChild:String;
		private var RoomParent:String;
		
		override public function execute(notification:INotification) : void
		{
			filesData = facade.retrieveProxy(FilesProxy.NAME) as FilesProxy;
			sfsData = facade.retrieveProxy(SFSProxy.NAME) as SFSProxy;
			
			routeNotification(CommonConstants.MODULE_INVOKE, ["-clear"], null, "*");
			routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["freeze", true]], null, CommonConstants.MODULE_EVENT + "/*");
			
			for (var i:int = 0; i < notification.getBody()[1][1].length; i++)
			{
				switch (notification.getBody()[1][1][i].name)
				{
					case "Music":
						Music = notification.getBody()[1][1][i].value;
						break;
					case "BackGround":
						BackGround = notification.getBody()[1][1][i].value;
						break;
					case "Canton":
						Canton = notification.getBody()[1][1][i].value;
						break;
					case "County":
						County = notification.getBody()[1][1][i].value;
						break;
					case "Fort":
						Fort = notification.getBody()[1][1][i].value;
						break;
					case "Place":
						Place = notification.getBody()[1][1][i].value;
						break;
					case "RoomChild":
						RoomChild = notification.getBody()[1][1][i].value;
						break;
					case "RoomParent":
						RoomParent = notification.getBody()[1][1][i].value;
						break;
				}
			}
			
			RoomID = notification.getBody()[1][0];
			RoomType = int(RoomID.split("-")[0]);
			
			switch (RoomType)
			{
				case 1:
					RoomTypeName = "Canton";
					RoomName = Canton;
					break;
				case 2:
					RoomTypeName = "County";
					RoomName = County;
					break;
				case 3:
					RoomTypeName = "Fort";
					RoomName = Fort;
					break;
				case 4:
					RoomTypeName = "Place";
					RoomName = Place;
					break;
				case 5:
					RoomTypeName = "House";
					RoomName = Place;
					break;
				case 6:
					RoomTypeName = "Challenge";
					RoomName = Place;
					break;
				case 7:
					RoomTypeName = "Argue";
					RoomName = Place;
					break;
			}
			
			Music = "/SanGuo/sound/music/" + Music;
			BackGround = "/SanGuo/shell/background/" + RoomTypeName + "/" + BackGround;
			
			sendNotification(CommonConstants.FILELOADER, ["/fileloader -D", Music]);
			sendNotification(CommonConstants.FILELOADER, ["/fileloader -D", BackGround]);
			
			var checkTimer:Timer = new Timer(1000);
			checkTimer.addEventListener(TimerEvent.TIMER, function(evt:TimerEvent):void {_tryJoinRoom(evt, [Music, BackGround]);});
			checkTimer.start();
		}
		
		//Custom
		private function _tryJoinRoom(evt:TimerEvent, fileNeed:Array):void
		{
			var checkMusic:Boolean = false;
			var checkBackGround:Boolean = false;
			
			var Music:ByteArray = new ByteArray();
			var BackGround:ByteArray = new ByteArray();
			
			for (var i:int = 0; i < filesData.filesArray.length; i++)
			{
				if (fileNeed[0] == filesData.filesArray[i][0])
				{
					Music.writeBytes(filesData.filesArray[i][1]);
					
					checkMusic = true;
				}
				
				if (fileNeed[1] == filesData.filesArray[i][0])
				{
					BackGround.writeBytes(filesData.filesArray[i][1]);
					
					checkBackGround = true;
				}
			}
			
			if (checkMusic && checkBackGround)
			{
				evt.target.stop();
				evt.target.removeEventListener(TimerEvent.TIMER, _tryJoinRoom);
				
				routeNotification(CommonConstants.MODULE_INVOKE, ["-close", "*"], null, CommonConstants.MODULE_ACTION + "/*");
				routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["freeze", false]], null, CommonConstants.MODULE_EVENT + "/*");
				routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["roomName", RoomID]], null, CommonConstants.MODULE_EVENT + "/*");
				routeNotification(CommonConstants.MODULE_INVOKE, ["-place", [fileNeed[1], BackGround, RoomID]], null, CommonConstants.MODULE_BACKGROUND + "/*");
				routeNotification(CommonConstants.MODULE_INVOKE, ["-music", [fileNeed[0], Music]], null, CommonConstants.MODULE_ZONE + "/*");
				routeNotification(CommonConstants.MODULE_INVOKE, ["-text", Canton + " " + County + " " + Fort + " " + Place], null, CommonConstants.MODULE_ZONE + "/*");
				routeNotification(CommonConstants.MODULE_INVOKE, ["-load", sfsData.sfs.lastJoinedRoom.userList], null, CommonConstants.MODULE_CROWD + "/*");
				
				if (RoomType == 3 || RoomType == 4)
				{
					routeNotification(CommonConstants.MODULE_INVOKE, ["-build", [RoomParent, RoomChild]], null, CommonConstants.MODULE_ROOM + "/*");
				}
				
				if (RoomType == 6 || RoomType == 7)
				{
					routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["show", true]], null, CommonConstants.MODULE_ACTION + "/*");
					routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["show", true]], null, CommonConstants.MODULE_BACKGROUND + "/*");
					routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["show", false]], null, CommonConstants.MODULE_CHARACTER + "/*");
					routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["show", false]], null, CommonConstants.MODULE_CHAT + "/*");
					routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["show", false]], null, CommonConstants.MODULE_CROWD + "/*");
					routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["show", true]], null, CommonConstants.MODULE_EVENT + "/*");
					routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["show", true]], null, CommonConstants.MODULE_PROMPT + "/*");
					routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["show", true]], null, CommonConstants.MODULE_ROOM + "/*");
					routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["chat", false]], null, CommonConstants.MODULE_ROOM + "/*");
					routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["show", true]], null, CommonConstants.MODULE_TOOL + "/*");
					routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["show", true]], null, CommonConstants.MODULE_ZONE + "/*");
				}
				else
				{
					routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["show", true]], null, "*");
					routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["chat", true]], null, CommonConstants.MODULE_ROOM + "/*");
					routeNotification(CommonConstants.MODULE_INVOKE, ["-msg", ["", "你已进入 " + RoomName + " (当前位置" + sfsData.sfs.lastJoinedRoom.userList.length + "人)", 0]], null, CommonConstants.MODULE_CHAT + "/*");
				}
				
				Music.clear();
				BackGround.clear();
			}
			else
			{
				sendNotification(CommonConstants.SYSTEM_CONSOLE, ["-gameStatus", [1, "系统处理中，请稍候..."]]);
			}
		}
	}
}